package ch.fhnw.qualiscan.application.modals;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.export.Excel;
import ch.fhnw.qualiscan.export.Pdf;
import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class Export extends Modal {
	
	OptionGroup exportOption;
	SurveyUserRole sur;
	
	public Export(SurveyUserRole sur) {
		this.sur = sur;
		
		VerticalLayout exportWrap = new VerticalLayout();
		exportWrap.setMargin(true, false, true, false);
		exportWrap.setSpacing(true);
		
		setCaption(TM.get("action.export"));
		addComponent(new Label(TM.get("export.desc")));
		
		List<Integer> weights = Arrays.asList(new Integer[] {0});
		exportOption = new OptionGroup(null, weights);
		
		//exportOption.setItemCaption(0, TM.get("export.pdf"));
		exportOption.setItemCaption(0, TM.get("export.excel"));
		
		
		Button exportButton = new Button(TM.get("action.export"));
		exportButton.setIcon(Config.ICON_EXPORTSAVE);
		exportButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				Export export = (Export) event.getButton().getData();
				SurveyUserRole sur = export.sur;
				if (exportOption.getValue() != null) {
					if(Integer.parseInt(exportOption.getValue().toString()) == 0) {
						final Excel excelSheet = new Excel(sur);
						openExcel(excelSheet);
					} else if (Integer.parseInt(exportOption.getValue().toString()) == 1) {
						final Pdf pdfDocument = new Pdf();
						String now = new SimpleDateFormat("yyyy-MM-dd_HHmmss").format(new Date());
						openPdf(pdfDocument, "QualiScan" + "_" + now);
					}	
				}
				export.close();
			}
		});
		exportButton.setData(this);
		exportWrap.addComponent(exportOption);
		exportWrap.addComponent(exportButton);
		
		
		addComponent(exportWrap);
	}
	
	public void openExcel(StreamSource streamSource) {
		Window mainWindow = TPTApplication.getCurrentApplication().getMainWindow();
		String now = new SimpleDateFormat("yyyy-MM-dd_HHmmss").format(new Date());
		StreamResource streamResource = new StreamResource(streamSource, "QualiScan_" + now + ".xls", TPTApplication.getCurrentApplication());
		streamResource.setCacheTime(2000);
		streamResource.setMIMEType("application/vnd.ms-excel");
		mainWindow.open(streamResource, "_top");
	}
	
	/**
	 * Exports a PDF - not used at this point.
	 * @param streamSource
	 * @param filename
	 */
	public void openPdf(StreamSource streamSource, final String filename) {
		Window mainWindow = TPTApplication.getCurrentApplication().getMainWindow();
		
		StreamResource streamResource = new StreamResource(streamSource, filename + ".pdf", TPTApplication.getCurrentApplication());
		streamResource.setCacheTime(2000);
		streamResource.setMIMEType("application/pdf");
		mainWindow.open(streamResource, "_self");
	}
	

}

